/* $Id : shopping_flow.js 4865 2007-01-31 14:04:10Z paulgao $ */
var selectedShipping = null;
var selectedPayment  = null;
var selectedPack     = null;
var selectedCard     = null;
var selectedSurplus  = '';
var selectedBonus    = 0;
var selectedIntegral = 0;
var selectedOOS      = null;
var alertedSurplus   = false;
var groupBuyShipping = null;
var groupBuyPayment  = null;
var selectedUcardPWD     = 0;


/* *
 * 改变配送方式
 */
function selectShipping(obj)
{
  if (selectedShipping == obj)
  {
    return;
  }
  else
  {
    selectedShipping = obj;
  }
  
  var shipping_id=obj.value;
  
  if(shipping_id==9)
  {
  	$("#bdate_00").blur();
  }
  
  var supportCod = obj.attributes['supportCod'].value + 0;
  var theForm = obj.form;
  for (i = 0; i < theForm.elements.length; i ++ )
  {
    if (theForm.elements[i].name == 'payment' && theForm.elements[i].attributes['isCod'].value == '1')
    {
      if (supportCod == 0)
      {
        theForm.elements[i].checked = false;
        theForm.elements[i].disabled = true;
      }
      else
      {
        theForm.elements[i].disabled = false;
      }
    }
  }
  if (obj.attributes['insure'].value + 0 == 0)
  {
    document.getElementById('ECS_NEEDINSURE').checked = false;
    document.getElementById('ECS_NEEDINSURE').disabled = true;
  }
  else
  {
    document.getElementById('ECS_NEEDINSURE').checked = false;
    document.getElementById('ECS_NEEDINSURE').disabled = false;
  }
}

/* 
 * 改变支付方式
 */
function selectPayment(obj)
{  
	obj = $(obj);
	type = obj.val();
	switch(type)
	{
		case '1':
  			ret = Ajax.call('index.php?action=user.getMoney', false, 'GET', 'TEXT', false, false);
			if(parseInt(ret) != ret)
				alert(ret);
			else
			{
				html = obj.next().html();
				obj.next().html(html+'(当前'+ret+')');
				if(ret < cur_order_amount)
				{
					alert('余额不足');
					obj.attr("disabled", true);
				}
				else
					return;
			}
			$($("[name='payment']").eq(2)).attr("checked", true);
			break;
	}
}

function changeUcard()
{
	var card_pwd=$("#ECS_UCARD_PWD").attr('value');
	if(card_pwd.length != 27)
	{
		alert('请输入27位合法的序列号');
		return;
	}
 	if(selectedUcardPWD == card_pwd)
 		return;
	ret = Ajax.call('index.php?action=system.checkvoucher&no=' + card_pwd, '',  false, 'GET', 'json', false, false);
	if(!ret)
	{
		alert(Ajax.call('index.php?action=system.checkvoucher&no=' + card_pwd, '',  false, 'GET', 'TEXT', false, false));
		return;
	}
 	selectedUcardPWD = card_pwd;
	if(ret.msg != '')
		msg = ret.msg;
	else
	{
		msg = '金额：'+ret.amount + '\n';
		if(ret.lefttime > 60)
			msg += '未过期';
		else
			msg += ret.lefttime + '分钟过期';
		cur_dj_amount = parseInt(ret.amount);
		ck_dj_msg = '';
		if(order_amount_update())
			clean_jf();
	}
	alert(msg);
}

/* *
 * 改变发票的方式
 */
function changeNeedInv()
{
  var obj        = document.getElementById('ECS_NEEDINV');
  var objType    = document.getElementById('ECS_INVTYPE');
  var objPayee   = document.getElementById('ECS_INVPAYEE');
  var objContent = document.getElementById('ECS_INVCONTENT');
  var needInv    = obj.checked ? 1 : 0;
  var invType    = obj.checked ? (objType != undefined ? objType.value : '') : '';
  var invPayee   = obj.checked ? objPayee.value : '';
  var invContent = obj.checked ? objContent.value : '';
  objType.disabled = objPayee.disabled = objContent.disabled = ! obj.checked;
  if(objType != null)
  {
    objType.disabled = ! obj.checked;
  }
}

/* *
 * 检查提交的订单表单
 */
function checkOrderForm(frm)
{ 
  //检查是否已登陆
  //检查时间
  //检查商品是否能单独购买
  if(ck_dj_msg != '' || ck_jf_msg)
  {
	  alert(ck_dj_msg+'\n'+ck_jf_msg);
	  return false;
  }
  
  //检查是否选择巧克力牌
  var need_card_birthday=$("#need_card_birthday").attr('value');
  
  if(need_card_birthday==1)
  {
		var card_birthday=$("#card_birthday").attr('value');
		if(card_birthday=='')
		{
		    alert(flow_no_card_birthday);
		    return false;			
		}
  }
  //检查是否输入了祝福卡贺语
  var need_card=$("#need_card").attr('value');
  
  if(need_card==1)
  {
		var card_message=$("#card_message").attr('value');
		if(card_message=='' || card_message=='写下您的祝福语')
		{
		    alert(flow_no_card_message);
		    return false;			
		}
  } 
  
  //会员特惠日不能使用代金卡和余额支付

  var paymentSelected = false;
  var shippingSelected = false;
  
  // 检查是否选择了支付配送方式
  for (i = 0; i < frm.elements.length; i ++ )
  {
    if (frm.elements[i].name == 'shipping' && frm.elements[i].checked)
    {
      shippingSelected = true;
    }
    if (frm.elements[i].name == 'payment' && frm.elements[i].checked)
    {
      paymentSelected = true;
    }
  }
  if ( ! shippingSelected)
  {
    alert(flow_no_shipping);
    return false;
  }
  if ( ! paymentSelected)
  {
    alert(flow_no_payment);
    return false;
  }
  // 检查用户输入的余额
  if (document.getElementById("ECS_SURPLUS"))
  {
    var surplus = document.getElementById("ECS_SURPLUS").value;
    var error   = false; //Utils.trim(Ajax.call('?step=check_surplus', 'surplus=' + surplus, null, 'GET', 'TEXT', false));
    if (error)
    {
      try
      {
        document.getElementById("ECS_SURPLUS_NOTICE").innerHTML = error;
      }
      catch (ex)
      {
      }
      return false;
    }
  }
  // 检查用户输入的积分
  if (document.getElementById("ECS_INTEGRAL"))
  {
    var integral = document.getElementById("ECS_INTEGRAL").value;
    var error    = false; //Utils.trim(Ajax.call('?step=check_integral', 'integral=' + integral, null, 'GET', 'TEXT', false));
    if (error)
    {
      return false;
      try
      {
        document.getElementById("ECS_INTEGRAL_NOTICE").innerHTML = error;
      }
      catch (ex)
      {
      }
    }
  }
  //检查是否输入了发票抬头
  if(document.getElementById("ECS_NEEDINV").checked)
  {
  	if(document.getElementById("ECS_INVPAYEE").value=='')
  	{
  		alert('请输入发票抬头。');
  		return false;
  	}
  }
  //填写正式地址
  var shiping = $('input[name=shipping]:checked').val();
  switch(parseInt(shiping))
  {
	  case 4:
		  var obj = $("[name='consignee_add']:checked");
		  type = parseInt(obj.val());
		  if(type == 0)
		  {
			  addr = region.GetAddress($("#selProvince_00").val(), $("#selCity_00").val(), $("#selDistricts_00").val(), $("#selArea_00").val());
			  $("#true_area").val(addr); 
			  $("#true_address").val($("#address_3").val()); 
			  $("#true_consignee").val($("#consignee_3").val()); 
			  $("#true_mobile_to").val($("#tel_3").val());
			  $("#true_mobile_from").val($("#mobile_3").val());
			  $("#true_sign_building").val($("#sign_building_3").val());
		  }
		  else
		  {
			  $("#true_area").val(eval("area_"+type)); 
			  $("#true_address").val(eval("addr_"+type)); 
			  $("#true_consignee").val(eval("consignee_" + type));
			  $("#true_mobile_to").val(eval("mobile_to_" + type));
			  $("#true_mobile_from").val(eval("mobile_from_" + type));
		  }
		  $("#true_delivery_type").val(1);
		  break;
	  case 9:
		  $("#true_area").val(area_come); 
		  $("#true_address").val(addr_come);
		  $("#true_consignee").val($("#consignee_come").val()); 
		  $("#true_mobile_to").val($("#tel_come").val());
		  $("#true_mobile_from").val($("#mobile_come").val());
		  $("#true_sign_building").val("");
		  $("#true_delivery_type").val(2);
		  break;
	  default:
		  alert("未选择收货地址??");
		  return false;
  }
  /*
  alert($("#true_area").val() + "\n" + $("#true_address").val() + "\n" + $("#true_consignee").val() + "\n"
		  + $("#true_mobile_to").val() + "\n" + $("#true_mobile_from").val()+ "\n"
		  + $("#true_sign_building").val() + "\n"
		  + $("#true_delivery_type").val());
  return false;
  */
  $("#dis_add_info").remove();
  frm.action = frm.action + '&step=done';
  return true;
}
/* *
 * 检查收货地址信息表单中填写的内容
 */
function checkConsignee(frm)
{
	var msg = new Array();
	var err = false;
	if (frm.elements['province'] && frm.elements['province'].value == 0 && frm.elements['province'].length > 1)
	{
		err = true;
		msg.push(province_not_null);
	}
	if (frm.elements['city'] && frm.elements['city'].value == 0 && frm.elements['city'].length > 1)
	{
		err = true;
		msg.push(city_not_null);
	}
	if (frm.elements['district'] && frm.elements['district'].length > 1)
	{
		if (frm.elements['district'].value == 0)
		{
			err = true;
			msg.push(district_not_null);
		}
	}
	if (frm.elements['area'] && frm.elements['area'].value == 0 && frm.elements['area'].length > 1)
	{
		err = true;
		msg.push(area_not_null);
	}
	if (Utils.isEmpty(frm.elements['consignee'].value))
	{
		err = true;
		msg.push(consignee_not_null);
	}
	if (frm.elements['address'] && Utils.isEmpty(frm.elements['address'].value))
	{
		err = true;
		msg.push(address_not_null);
	}
	if (frm.elements['zipcode'] && frm.elements['zipcode'].value.length > 0 && (!Utils.isNumber(frm.elements['zipcode'].value)))
	{
		err = true;
		msg.push(zip_not_num);
	}
	if (frm.elements['mobile_to'] && !Utils.isTel(frm.elements['mobile_to'].value))
	{
		err = true;
		msg.push(mobile_to_err);
	}
	if (frm.elements['mobile_from'] && !Utils.isTel(frm.elements['mobile_from'].value))
	{
		err = true;
		msg.push(mobile_from_err);
	}
	if (err)
	{
		message = msg.join("\n");
		alert(message);
		return false;
	}
	return true;
}

function changeCnt(number, key)
{
	number = parseInt(number);
	if(isNaN(number) || number <= 0)
	{
		alert("输入错误");
		return;
	}
	Ajax.call('index.php?action=cart.changecnt', 'key=' + key + '&cnt=' + number, changeCntResponse, 'GET', 'JSON');
}

/**
 * 接收返回的信息
 */
function changeCntResponse(res)
{
	switch(res.ret)
	{
		case 0:
			$("#cart_" + res.key).val(res.val);
			$("#cart_" + res.key).val(res.val);
			$("#total_" + res.key).text('￥' + res.total + '元');
			break;
		case 1:
			alert("该商品已经移除购物车");
			$("#cart_tr_" + res.key).remove();
			break;
		default:
			alert("err:\n" + res.val);
	}
}
